import {updateAvatar, modifyUser, modifyPass} from '@/services/system/SysUser';
import {getLoginUserInfo} from '@/services/system/SysLogin';
import {message} from 'antd';

export default {
  namespace: 'PUcenter',

  state: {
    userInfo: undefined,
  },

  effects: {
    * getLoginUserInfo({}, {call, put}) {
      const response = yield call(getLoginUserInfo);
      if (response.code === 0) {
        yield put({
          type: 'chgUserInfo',
          payload: response.data,
        });
      }
    },

    * updateAvatar({payload, callback}, {call}) {
      const response = yield call(updateAvatar, payload);
      if (response.code === 0) {
        message.success('更新成功').then();
        callback && callback();
      }
    },

    * modifyUser({payload, callback}, {call}) {
      const response = yield call(modifyUser, payload);
      if (response.code === 0) {
        message.success('更新成功').then();
        callback && callback();
      }
    },

    * modifyPass({payload, callback}, {call}) {
      const response = yield call(modifyPass, payload);
      if (response.code === 0) {
        message.success('更新成功').then();
        callback && callback();
      }
    },

  },

  reducers: {
    chgUserInfo(state, action) {
      return {
        ...state,
        userInfo: action.payload,
      };
    },
  },
};
